const query = require('./fun')
const sqlCount = 'SQL_CALC_FOUND_ROWS';
const { ADMIN, USER, REGION, ORDERS } = require('./database')
let { sqlUpdateStr, sqlAddStr, sqlSelectStr, sqlWhereStr } = require('../util.js');
/**
 * 所有请求方法
 */
/**
 *  登录
 */
function isAdminLogin(data) {
    return new Promise((resolve, reject) => {
        query(`select  * from ${ADMIN} where name=? and password=?`, data)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取管理员全部信息
 */
function getAdminList(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        query(`select ${sqlCount} * from ${ADMIN} ${condition}  limit ?,${limit}`, { num }, true)
            .then(async (res) => {
                resolve(res);
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取分页用户数
 */
function getUserList(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        query(`select  ${sqlCount} * from ${USER} ${condition}  limit ?,${limit}`, { num }, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 修改用户状态
 */
function setUserState(userId, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlUpdateStr(sql, data);
        console.log(sql)
        query(`UPDATE ${USER} SET ${sql} WHERE userId=${userId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    });
}
/**
 * 删除用户
 */
function delUser(userId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${USER} WHERE userId in (${String(userId)})`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取澡堂
 */
function getRegionList(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        query(`select  ${sqlCount} * from ${REGION} ${condition}  limit ?,${limit}`, { num }, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 添加澡堂
 */
function addRegion(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${REGION}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取指定澡堂
 */
function getRegionById(regionId) {
    return new Promise((resolve, reject) => {
        query(`select * from ${REGION} where regionId=?`, { regionId })
            .then((res) => {
                resolve(res[0])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 删除澡堂
 */
function delRegion(regionId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${REGION} WHERE regionId in (${String(regionId)})`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 修改澡堂
 */
function updateRegion(regionId, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlUpdateStr(sql, data);
        query(`UPDATE ${REGION} SET ${sql} WHERE regionId=${regionId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取数据数目
 */
function getIndexCount() {
    return new Promise((resolve, reject) => {
        query([`select count(*) as userNum from ${USER}`, `select count(*) as adminNum from ${ADMIN}`, `select count(*) as regionNum from ${REGION}`, `select count(*) as orderNum from ${ORDERS}`])
            .then((res) => {
                let arr = res.map(item => item[0])
                resolve(arr)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取一周用户数据
 */
function getUserListWord() {
    return new Promise((resolve, reject) => {
        query(`select  ${sqlCount} * from ${USER} where YEARWEEK(date_format(userTime,'%Y-%m-%d'),1) = YEARWEEK(now(),1)`, {}, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    });
}
/**
 * 获取澡堂分类数据
 */
function getRegionTable(typeList) {
    let sql = []
    sql = typeList.map(regionState => {
        return `select count(regionId)  value,'${regionState}'  name from ${REGION} where regionState='${regionState}'`
    })
    return new Promise((resolve, reject) => {
        query(sql)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
exports.isAdminLogin = isAdminLogin;
exports.getAdminList = getAdminList;
exports.getUserList = getUserList;
exports.setUserState = setUserState;
exports.delUser = delUser;
exports.getRegionList = getRegionList;
exports.addRegion = addRegion;
exports.getRegionById = getRegionById;
exports.delRegion = delRegion;
exports.updateRegion = updateRegion;
exports.getIndexCount = getIndexCount;
exports.getUserListWord = getUserListWord;
exports.getRegionTable = getRegionTable;